//
//  LGTabBarViewController.m
//  Eluxiao
//
//  Created by Chen on 16/8/19.
//  Copyright © 2016年 langan. All rights reserved.
//

#import "LGTabBarViewController.h"
#import "LGNavigationController.h"
//首页
#import "LGHomeViewController.h"
//在线咨询
#import "LGConsultHomeController.h"
//患者
#import "LGPatientHomeController.h"
//我的
#import "LGMineViewController.h"

//报告上传
#import "LGUploadFilesController.h"

@interface UIImage (Extension)
// 快速的返回一个最原始的图片
+ (instancetype)imageWithOriRenderingImage:(NSString *)imageName;
@end

@interface LGTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation LGTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [UITabBar appearance].translucent = NO;
    //默认颜色、字体
    [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithString:@"171417"],NSForegroundColorAttributeName,[UIFont systemFontOfSize:11],NSFontAttributeName,nil]forState:UIControlStateNormal];
    //选中颜色、字体
    [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithString:@"#429BFF"],NSForegroundColorAttributeName,[UIFont systemFontOfSize:11],NSFontAttributeName,nil]forState:UIControlStateSelected];
    //iOS 13  文字颜色变蓝色
    if(@available(iOS 13.0, *)) {
        [[UITabBar appearance] setTintColor:[UIColor colorWithString:@"02BE61"]];
        [[UITabBar appearance] setUnselectedItemTintColor:[UIColor colorWithString:@"171417"]];
    }
    
    //首页
    [self addOneViewController:[[LGHomeViewController alloc] init] image:@"homeNormal" selectedImage:@"homeSelect" title:@"工作台"];
    //在线咨询
    [self addOneViewController:[[LGConsultHomeController alloc] init] image:@"consultNormal" selectedImage:@"consultSelect" title:@"在线咨询"];
    //患者管理
    [self addOneViewController:[[LGPatientHomeController alloc]init] image:@"patientNormal" selectedImage:@"patientSelect" title:@"患者管理"];
    //我的
    [self addOneViewController:[[LGMineViewController alloc]init] image:@"mineNormal" selectedImage:@"mineSelect" title:@"我的"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground)  name:UIApplicationWillEnterForegroundNotification object:nil];
}

//APP进入前台
-(void)willEnterForeground{
    LGNavigationController *navController = self.selectedViewController;
    if (navController) {
        LGBaseViewController *controller = (LGBaseViewController *)navController.viewControllers.firstObject;
        if (controller) {
            [controller viewWillAppearAction];
        }
    }
}

- (void)addOneViewController:(UIViewController *)childViewController image:(NSString *)imageName selectedImage:(NSString *)selectedImageName title:(NSString *)title{
    LGNavigationController *nav = [[LGNavigationController alloc] initWithRootViewController:childViewController];
    //设置图片和文字之间的间距
    nav.tabBarItem.imageInsets = UIEdgeInsetsMake(-2, 0, 2, 0);
    nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -4);
    
    //设置tabBar文字
    nav.tabBarItem.title = title;
    //设置正常状态下的图标
    nav.tabBarItem.image = [UIImage imageWithOriRenderingImage:imageName];
    //设置选中状态下的图标
    nav.tabBarItem.selectedImage = [UIImage imageWithOriRenderingImage:selectedImageName];
    //添加tabBar为控制器的子控制器
    [self addChildViewController:nav];
}

-(void)goToUploadFile:(NSURL *)fileUrl{
    self.selectedIndex = 3;
    LGNavigationController *navController = self.selectedViewController;
    if ([[navController.viewControllers lastObject] isKindOfClass:[LGUploadFilesController class]]) {
        LGUploadFilesController *fileController = (LGUploadFilesController *)[navController.viewControllers lastObject];
        [fileController uploadFileAction:fileUrl];
        
    }else{
        LGUploadFilesController *fileController = [[LGUploadFilesController alloc]init];
        [self.selectedViewController  pushViewController:fileController animated:YES];
        [fileController uploadFileAction:fileUrl];
    }
}


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    UINavigationController *nav = self.selectedViewController;
//    UIViewController *controller = nav.viewControllers[0];
    
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}


@end



@implementation UIImage (Extension)

// 快速的返回一个最原始的图片
+ (instancetype)imageWithOriRenderingImage:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

@end


